package com.simon.nvwo.common.kit.email;

import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
import org.apache.commons.mail.SimpleEmail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.MalformedURLException;
import java.util.Collection;


/**
 * 发邮件工具类
 */

@SuppressWarnings("unchecked")
public class EmailSender {
	
	private static final Logger log = LoggerFactory.getLogger(EmailSender.class);
	
	//application.properties
	private final static String EMAIL_HOSTNAME = PropertiesUtil.getValueByKey("hostname");//邮箱服务器，如：smtp.qq.com
	private final static String EMAIL_USERNAME = PropertiesUtil.getValueByKey("username");//邮箱账户，如：xxx@qq.com
	private final static String EMAIL_PASSWORD = PropertiesUtil.getValueByKey("password");//邮箱密码
	private final static String EMAIL_NAME = PropertiesUtil.getValueByKey("name");//发件人名称（昵称）
	private final static boolean EMAIL_IS_SSL = "true".equalsIgnoreCase(PropertiesUtil.getValueByKey("email_is_ssl"));//是否支持SSL链接
	private final static String CHARSET_NAME="UTF-8";
	private final static String ATTACH_NAME="附件";
	private final static String DESCRIPTION="邮件附件";
	
	public static void main(String[] args) throws MalformedURLException, EmailException {
		
/*		String path=new String("pictures/123.jpg"); //附件位置,绝对路径
*/		/*URL url=new URL("http://blog.csdn.net/chenleixing/article/details/44994631");*/
		
		//发送简单文本邮件
		/*sendSimpleEmail("123", "123", "wu1258516653@163.com");*/
		
		//发送带有classpath路径下的附件的邮件
		String path=new String("pictures/123.jpg");
		
		//发送带有本地附件的邮件
		/*String path=new String("D:\\abc.txt");*/
		sendMultiPartEmail("123", "123", "wu1258516653@163.com",path);

		
	}
	/**
	 * 发送纯文本邮件
	 * @param subject 主题
	 * @param msg 内容
	 * @param to 主送
	 */
	public static void sendSimpleEmail(String subject, String msg, Object to){
		sendSimpleEmail(subject, msg, to, null, null);
	}
	
	/**
	 * 发送纯文本邮件
	 * @param subject 主题
	 * @param msg 内容
	 * @param to 主送
	 * @param cc 抄送
	 */
	public static void sendSimpleEmail(String subject, String msg, Object to, Object cc){
		sendSimpleEmail(subject, msg, to, cc, null);
	}
	
	/**
	 * 发送纯文本邮件
	 * @param subject 主题
	 * @param msg 内容
	 * @param to 主送
	 * @param cc 抄送
	 * @param bcc 密送
	 */
	public static void sendSimpleEmail(String subject, String msg, Object to, Object cc, Object bcc){
		SimpleEmail email = new SimpleEmail();
		email.setHostName(EMAIL_HOSTNAME);
		email.setAuthentication(EMAIL_USERNAME, EMAIL_PASSWORD);
		email.setSSLOnConnect(EMAIL_IS_SSL);
		email.setCharset(CHARSET_NAME);

		
		email.setSubject(subject);
		try {
			email.setFrom(EMAIL_USERNAME, EMAIL_NAME);
			
			if(to instanceof String){
				email.addTo((String) to);
			}else if(to instanceof String[]){
				email.addTo((String[]) to);
			}else if(to instanceof Collection){
				email.addTo(((Collection<String>) to).toArray(new String[0]));
			}
			
			if (cc != null) {
				if(cc instanceof String){
					email.addCc((String) cc);
				}else if(cc instanceof String[]){
					email.addCc((String[]) cc);
				}else if(cc instanceof Collection){
					email.addCc(((Collection<String>) cc).toArray(new String[0]));
				}
			}
			
			if (bcc != null) {
				if(bcc instanceof String){
					email.addBcc((String) bcc);
				}else if(bcc instanceof String[]){
					email.addBcc((String[]) bcc);
				}else if(bcc instanceof Collection){
					email.addBcc(((Collection<String>) bcc).toArray(new String[0]));
				}
			}
			
			email.setMsg(msg);
			
			email.send();
			
			if(log.isInfoEnabled()){
				log.info("email sent success!");
			}
		} catch (EmailException e) {
			log.error(e.getMessage(), e);
		}
	}
	
	
	/**
	 * 发送带附件的邮件
	 * @param subject 主体
	 * @param msg	内容
	 * @param to	主送
	 * @param path	附件path
	 */
	public static void sendMultiPartEmail(String subject,String msg,Object to,String path){

			sendMultiPartEmail(subject, msg, to, path,null,null);
			

		
	}
	
	/**
	 * 发送带附件的邮件
	 * @param subject 主体
	 * @param msg	内容
	 * @param to	主送
	 * @param path	附件path
	 * @param cc	抄送
	 */
	public static void sendMultiPartEmail(String subject,String msg,Object to,String path,Object cc){
		
		sendMultiPartEmail(subject, msg, to, path,cc,null);
	}
	
	 /* 发送带附件的邮件
	 * @param subject 主体
	 * @param msg	内容
	 * @param to	主送
	 * @param path	附件path
	 * @param cc	抄送
	 * @param bcc 	密送
	 * */
	public static void sendMultiPartEmail(String subject,String msg,Object to,String path,Object cc,Object bcc){

	    EmailAttachment attachment = new EmailAttachment(); 
		
		attachment.setPath(path); 
		
		attachment.setDisposition(EmailAttachment.ATTACHMENT);  
		
		attachment.setDescription(DESCRIPTION);  
		
		attachment.setName(ATTACH_NAME); 

		  // 创建电子邮件  
		 MultiPartEmail email = new MultiPartEmail();  
		 email.setHostName(EMAIL_HOSTNAME);  
		 
		 email.setAuthentication(EMAIL_USERNAME, EMAIL_PASSWORD); 
		 email.setSubject(subject);  
		   
		 email.setSSLOnConnect(EMAIL_IS_SSL);
		 email.setCharset(CHARSET_NAME);
		 
		
		 
		 try{
			 
			 if(to instanceof String){
					
					email.addTo((String) to);
				}else if(to instanceof String[]){
					email.addTo((String[])to);
				}else if(to instanceof Collection){
					
					email.addTo(((Collection<String>) to).toArray(new String[0]));
					
			}
			
			 if(cc instanceof String){
					
					email.addCc((String) cc);
				}else if(cc instanceof String[]){
					email.addTo((String[])cc);
				}else if(cc instanceof Collection){
					
					email.addTo(((Collection<String>) cc).toArray(new String[0]));
					
				}
				
				
				if(bcc instanceof String){
					
					email.addCc((String) bcc);
				}else if(bcc instanceof String[]){
					email.addTo((String[])bcc);
				}else if(bcc instanceof Collection){
					
					email.addTo(((Collection<String>) bcc).toArray(new String[0]));
					
				}
			
			 email.setFrom(EMAIL_USERNAME);
			 
			 email.setMsg(msg);
			  // 添加附件  
			 email.attach(attachment);  
			  // 发送邮件  
			 email.send(); 
			 
			 if(log.isInfoEnabled()){
				 
				 log.info("成功发送邮件");
			 }
		 }catch(EmailException e){
			 
			 log.info("发送邮件失败", e);
		 }

		
	}	
	
}
	/**
	 * 发送带附件的邮件
	 * @param subject 主体
	 * @param msg	内容
	 * @param to	主送
	 * @param url	附件url
	 */
/*	public static void sendMultiPartEmail(String subject,String msg,Object to,URL url){
	
		
		sendMultiPartEmail(subject, msg, to, url,null,null);
	}
	*/
	/**
	 * 发送带附件的邮件
	 * @param subject 主体
	 * @param msg	内容
	 * @param to	主送
	 * @param url	附件url
	 * @param cc	抄送
	 */
/*	public static void sendMultiPartEmail(String subject,String msg,Object to,URL url,Object cc){
		
		sendMultiPartEmail(subject, msg, to, url,cc,null);
	}
	*/
	/**
	 * 发送带附件（url）的邮件
	 * @param subject 主题
	 * @param msg	内容
	 * @param to	主送
	 * @param url	附件url
	 * @param cc	抄送
	 * @param bcc	密送
	 */
	/*public static void sendMultiPartEmail(String subject,String msg,Object to,URL url,Object cc,Object bcc){
		
		MultiPartEmail email=new MultiPartEmail();
		
		email.setAuthentication(EMAIL_USERNAME, EMAIL_PASSWORD);
		email.setSubject(subject);
		email.setHostName(EMAIL_HOSTNAME);
		email.setSSLOnConnect(EMAIL_IS_SSL);
		
		email.setCharset(CHARSET_NAME);
		
		try{
			
			email.setFrom(EMAIL_USERNAME, EMAIL_NAME);
			
			if(to instanceof String){
				
				email.addTo((String) to);
			}else if(to instanceof String[]){
				email.addTo((String[])to);
			}else if(to instanceof Collection){
				
				email.addTo(((Collection<String>) to).toArray(new String[0]));
				
			}
			
			
			
			if(cc instanceof String){
				
				email.addCc((String) cc);
			}else if(cc instanceof String[]){
				email.addTo((String[])cc);
			}else if(cc instanceof Collection){
				
				email.addTo(((Collection<String>) cc).toArray(new String[0]));
				
			}
			
			
			if(bcc instanceof String){
				
				email.addCc((String) bcc);
			}else if(bcc instanceof String[]){
				email.addTo((String[])bcc);
			}else if(bcc instanceof Collection){
				
				email.addTo(((Collection<String>) bcc).toArray(new String[0]));
				
			}
			
			email.setMsg(msg);
			
			EmailAttachment attachment=new EmailAttachment();
			
			attachment.setURL(url);
			attachment.setDisposition(EmailAttachment.ATTACHMENT);//定义附件位置
			attachment.setPath("D:\\MongoDB.txt");//本地附件，绝对路径    
			attachment.setName(ATTACH_NAME);
			attachment.setDescription(DESCRIPTION);
			
			email.send();
			if(log.isInfoEnabled()){
				
				log.info("email send success");
			}
		}catch(Exception e){
			
			log.info("email send fail", e);
		}
	}
	*/
	
	